<?php

namespace App\Repository\Models;

use Illuminate\Database\Eloquent\Model;
use Prettus\Repository\Contracts\Transformable;
use Prettus\Repository\Traits\TransformableTrait;
use Illuminate\Database\Eloquent\SoftDeletes;

use Illuminate\Support\Facades\Cache;
/**
 * Class ShopEmployee.
 *
 * @package namespace App\Repository\Models;
 */
class ShopEmployee extends Model implements Transformable
{
    use TransformableTrait;
    use SoftDeletes;
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $table='shop_employee';
    protected $fillable = ['id','user_id','user_phone','user_nick','shop_id','user_captcha','land_area_code','land_number','fax_area_code','fax_number','area_code','province_code','city_code','address_detail','post_code','position','sex'];

    protected $appends=['emp_id','province_name', 'city_name', 'area_name'];

    public function getEmpIdAttribute()
    {
       return $this->id;
    }

    public function user(){
        return $this->hasOne(User::class,'id','user_id');
    }

    public function shopRightAuth(){
        return $this->hasMany(ShopRightAuth::class,'emp_id','id');
    }

    public function getProvinceNameAttribute()
    {
        if($province_code = array_get($this->attributes, 'province_code')){
            $province = Cache::remember('regions:' . $province_code, 60 * 24, function() use($province_code){
                $province = Regions::query()->find($province_code);
                return $province ? $province->name : '';
            });
            return $province;
        }
        return '';
    }

    public function getCityNameAttribute()
    {
        if($city_code = array_get($this->attributes, 'city_code')){
            $city = Cache::remember('regions:' . $city_code, 60 * 24, function() use($city_code){
                $city = Regions::query()->find($city_code);
                return $city ? $city->name : '';
            });
            return $city;
        }
        return '';
    }

    public function getAreaNameAttribute()
    {
        if($area_code = array_get($this->attributes, 'area_code')){
            $area = Cache::remember('regions:' . $area_code, 60 * 24, function() use($area_code){
                $area = Regions::query()->find($area_code);
                return $area ? $area->name : '';
            });
            return $area;
        }
        return '';
    }

}
